<?php
/**
 * 功能: 积分管理
 * User: 文丹
 * Date: 2018/7/26 0026
 * Time: 17:56
 */

namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;

class Integral extends Power{
    /**
     * 积分记录表
     */
    public function record(){
        $records = Db::name('integral_record')->order('id desc')->paginate(10)->each(function($item,$key){
            $user = Db::name('user')->where('id',$item['user_id'])->find();
            $item['user_integral'] = $user['integral'];
            $item['nickname'] = $user['nickname'];
            $item['phone'] = $user['phone'];
            return $item;
        });
        $count   = Db::name('integral_record')->count();
        $this->assign([
            'records'=>$records,
            'count'=>$count,
            'empty'=>'<tr class="text-c va-m empty"><td colspan="8">暂无数据</td></tr>'
        ]);
        return view('');

    }
    /**
     * 记录搜索
     */
    public function search_record(){
        $common   = trim(input('common'));
        $datemin  = trim(input('datemin'));
        $datemax  = trim(input('datemax'));
        if(empty($common)&&empty($datemin)&&empty($datemax)){
            return json(['msg'=>'搜索天剑不能为空','ok'=>false]);
        }
        if(!empty($common)&& !empty($datemin)&& !empty($datemax)){
            $records = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','>=',$datemin)
                ->whereTime('ir.time','<=',$datemax)
                ->where('u.nickname','like','%'.$common.'%')
                ->whereOr('u.phone','like','%'.$common.'%')
                ->whereOr('ir.remark','like','%'.$common.'%')
                ->field('ir.*,u.integral as user_integral,u.nickname,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);
            /*echo "<pre>";
            print_r($records);
            die;*/
            $count  = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','>=',$datemin)
                ->whereTime('ir.time','>=',$datemax)
                ->where('u.nickname','like','%'.$common.'%')
                ->whereOr('u.phone','like','%'.$common.'%')
                ->whereOr('ir.remark','like','%'.$common.'%')
                ->count();
            if(!empty($records[0])){
                $this->assign([
                    'records'=>$records,
                    'count'=>$count
                ]);
                return $this->fetch('integral:record',['common'=>$common,'datemin'=>$datemin,'datemax'=>$datemax]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }


        }elseif(empty($common)&& !empty($datemin)&& !empty($datemax)){
            $records = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','>=',$datemin)
                ->whereTime('ir.time','<=',$datemax)
                ->field('ir.*,u.integral as user_integral,u.nickname,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);
            /*echo "<pre>";
            print_r($records);
            die;*/
            $count  = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','>=',$datemin)
                ->whereTime('ir.time','>=',$datemax)
                ->count();
            if(!empty($records[0])){
                $this->assign([
                    'records'=>$records,
                    'count'=>$count
                ]);
                return $this->fetch('integral:record',['common'=>$common,'datemin'=>$datemin,'datemax'=>$datemax]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }
        }elseif(empty($common)&& empty($datemin)&& !empty($datemax)){
            $records = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','<=',$datemax)
                ->field('ir.*,u.integral as user_integral,u.nickname,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);
            /*echo "<pre>";
            print_r($records);
            die;*/
            $count  = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','>=',$datemax)
                ->count();
            if(!empty($records[0])){
                $this->assign([
                    'records'=>$records,
                    'count'=>$count
                ]);
                return $this->fetch('integral:record',['common'=>$common,'datemin'=>$datemin,'datemax'=>$datemax]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }
        }elseif(empty($common)&& !empty($datemin)&& empty($datemax)){
            $records = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','>=',$datemin)
                ->field('ir.*,u.integral as user_integral,u.nickname,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);
            /*echo "<pre>";
            print_r($records);
            die;*/
            $count  = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','>=',$datemin)
                ->count();
            if(!empty($records[0])){
                $this->assign([
                    'records'=>$records,
                    'count'=>$count
                ]);
                return $this->fetch('integral:record',['common'=>$common,'datemin'=>$datemin,'datemax'=>$datemax]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }
        }if(!empty($common)&& empty($datemin)&& empty($datemax)){
            $records = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->where('u.nickname','like','%'.$common.'%')
                ->whereOr('u.phone','like','%'.$common.'%')
                ->whereOr('ir.remark','like','%'.$common.'%')
                ->field('ir.*,u.integral as user_integral,u.nickname,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);
            /*echo "<pre>";
            print_r($records);
            die;*/
            $count  = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->where('u.nickname','like','%'.$common.'%')
                ->whereOr('u.phone','like','%'.$common.'%')
                ->whereOr('ir.remark','like','%'.$common.'%')
                ->count();
            if(!empty($records[0])){
                $this->assign([
                    'records'=>$records,
                    'count'=>$count
                ]);
                return $this->fetch('integral:record',['common'=>$common,'datemin'=>$datemin,'datemax'=>$datemax]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }


        }if(!empty($common)&& !empty($datemin)&& empty($datemax)){
            $records = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','>=',$datemin)
                ->where('u.nickname','like','%'.$common.'%')
                ->whereOr('u.phone','like','%'.$common.'%')
                ->whereOr('ir.remark','like','%'.$common.'%')
                ->field('ir.*,u.integral as user_integral,u.nickname,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);
            /*echo "<pre>";
            print_r($records);
            die;*/
            $count  = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','>=',$datemin)
                ->where('u.nickname','like','%'.$common.'%')
                ->whereOr('u.phone','like','%'.$common.'%')
                ->whereOr('ir.remark','like','%'.$common.'%')
                ->count();
            if(!empty($records[0])){
                $this->assign([
                    'records'=>$records,
                    'count'=>$count
                ]);
                return $this->fetch('integral:record',['common'=>$common,'datemin'=>$datemin,'datemax'=>$datemax]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }


        }
        if(!empty($common)&& empty($datemin)&& !empty($datemax)){
            $records = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','<=',$datemax)
                ->where('u.nickname','like','%'.$common.'%')
                ->whereOr('u.phone','like','%'.$common.'%')
                ->whereOr('ir.remark','like','%'.$common.'%')
                ->field('ir.*,u.integral as user_integral,u.nickname,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);
            echo "<pre>";
            print_r($records);
            die;
            $count  = Db::name('integral_record')
                ->alias('ir')
                ->join('user u','u.id = ir.user_id')
                ->whereTime('ir.time','<=',$datemax)
                ->where('u.nickname','like','%'.$common.'%')
                ->whereOr('u.phone','like','%'.$common.'%')
                ->whereOr('ir.remark','like','%'.$common.'%')
                ->count();
            if(!empty($records[0])){
                $this->assign([
                    'records'=>$records,
                    'count'=>$count
                ]);
                return $this->fetch('integral:record',['common'=>$common,'datemin'=>$datemin,'datemax'=>$datemax]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }


        }


    }

    /**
     * 积分记录删出
     */
    public function record_del(){
        $id = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数','ok'=>false]);
        }
        $result = Db::name('integral_record')->where('id',$id)->delete();
        if($result){
            return json(['msg'=>'已删除','ok'=>true]);
        }else{
            return json(['msg'=>'删除失败','ok'=>false]);
        }

    }

}